/*
 * Created on 2008-3-7
 *
 * TODO 
 */
package jacky.lanlan.song.extension.struts.resource;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;

/**
 * A concrete implementation of the <code>Downloadable</code> interface which
 * represent a resource that can be downloaded from web.
 */
public class WebResource implements Downloadable {

	/**
   * The content type for this stream.
   */
	private String contentType;

	/**
	 * The resource name.
	 */
	private String name;
	
	/**
   * The servlet context for the resource to be downloaded.
   */
	private ServletContext context;

	/**
   * The path to the resource to be downloaded.
   */
	private String path;

	/**
   * Constructs an instance of this class, based on the supplied parameters.
   * 
   * @param contentType
   *          The content type of the file.
   * @param context
   *          The servlet context for the resource.
   * @param path
   *          The path to the resource to be downloaded.
   * @param name
   *          The resource name.
   */
	public WebResource(String contentType, ServletContext context,
			String path,String name) {
		this.contentType = contentType;
		this.context = context;
		this.path = path;
		this.name=name;
	}

	/**
   * Returns the content type of the stream to be downloaded.
   * 
   * @return The content type of the stream.
   */
	public String getContentType() {
		return this.contentType;
	}

	/**
   * Returns an input stream on the resource to be downloaded. This stream
   * will be closed by the <code>DownloadAction</code>.
   * 
   * @return The input stream for the resource to be downloaded.
   */
	public InputStream getInputStream() throws IOException {
		return context.getResourceAsStream(path);
	}
	
	public String getName() {
		return name;
	}
	
}
